//
//  AXMySrdzWdzwZwjdVC.m
//  AnXin100
//
//  Created by 天质弘耕 on 2018/11/10.
//  Copyright © 2018 tzhg. All rights reserved.
//

#import "AXMySrdzWdzwZwjdVC.h"
#import "ZXCategories.h"
#import "UIView+SDAutoLayout.h"
#import "AXMyNewFarmerNameCell.h"
#import "AXMyNewFarmerSexCell.h"
#import "AXMyNewFarmerPictureCell.h"
#import "AXMyNewFarmerSubmitCell.h"
#import "TZHGMyViewController.h"
#import "AXMyButtonSelectCell.h"
#import "AXMyTitleCell.h"
#import "AXMyAddressSelectCell.h"
#import "AXMyCheckProgressVC.h"
#import "AXMyFormerYearsDiseaseVC.h"
#import "MCPickerModel.h"
#import "MCPickerView.h"



/** 网络请求 */
#import "CZHAddressPickerView.h"
#import "WHRPublishPostARewardModel.h"
#import "zySheetPickerView.h"
#import "CGXPickerView.h"

@interface AXMySrdzWdzwZwjdVC ()<UITableViewDelegate, UITableViewDataSource,MCPickerViewDelegate>


@property (nonatomic , strong)MCPickerView *picker;
@property (nonatomic , strong)MCPickerView *picker1;
@property (nonatomic , strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *array;
///** 一级 */
//@property (nonatomic , strong)  __block NSMutableArray *province;
///** 二级 */
//@property (nonatomic , strong)  __block NSMutableArray *city;

/** 网络请求 */
@property (nonatomic , strong) CZHAddressPickerView *AddressPickerView ;
@property (nonatomic , strong) NSString *cropId;
//品种数组
@property (nonatomic, strong) NSMutableArray<WHRPublishPostARewardModel*>  * breedModelArray;
@property (nonatomic , strong) NSString *pz;
/** 品种id */
@property (nonatomic, strong) NSString *pzId;
@property (nonatomic , strong) NSString *timeStr;



@end

@implementation AXMySrdzWdzwZwjdVC


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self creatView];
    [self setlayout];
    [self setupNavigationItems];
    
    _array = @[@"间作", @"轮作", @"连作", @"套作"];
    
   
}


- (void)creatView {
    
    self.tableView = [[UITableView alloc]init];
    /** 去掉cell线 */
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    /** tableView滚动收键盘 */
    self.tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[AXMyNewFarmerNameCell class] forCellReuseIdentifier:[AXMyNewFarmerNameCell reuseID]];
    [self.tableView registerClass:[AXMyNewFarmerSexCell class] forCellReuseIdentifier:[AXMyNewFarmerSexCell reuseID]];
    [self.tableView registerClass:[AXMyNewFarmerPictureCell class] forCellReuseIdentifier:[AXMyNewFarmerPictureCell reuseID]];
    [self.tableView registerClass:[AXMyNewFarmerSubmitCell class] forCellReuseIdentifier:[AXMyNewFarmerSubmitCell reuseID]];
    [self.tableView registerClass:[AXMyButtonSelectCell class] forCellReuseIdentifier:[AXMyButtonSelectCell reuseID]];
    [self.tableView registerClass:[AXMyTitleCell class] forCellReuseIdentifier:[AXMyTitleCell reuseID]];
    [self.tableView registerClass:[AXMyAddressSelectCell class] forCellReuseIdentifier:[AXMyAddressSelectCell reuseID]];
    
    [self.view addSubview:self.tableView];
}


- (void)setlayout {
    
    self.tableView.sd_layout
    .leftSpaceToView(self.view, 0)
    .topSpaceToView(self.view, NAVIGATION_HEIGHT)
    .widthIs(Wi)
    .heightIs(TableViewHeight);
    
    
}


- (void)setupNavigationItems {
    
    /** 导航条左边的按钮 */
    //返回按钮
    UIButton *kfBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [kfBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    kfBtn.frame = CGRectMake(0, 0, 30, 30);
    [kfBtn addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithCustomView:kfBtn];
    self.navigationItem.leftBarButtonItem = leftItem;
    /** 搜索 */
    UILabel *titleLabel = [[UILabel alloc]init];
    titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];
    titleLabel.text = @"作物建档";
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.font = FONT_EIGHTEEN;
    titleLabel.textColor = [UIColor colorWithHex:0x29b566];
    self.navigationItem.titleView = titleLabel;
    
    
}

#pragma mark - request 网络请求



/** 品种 */
- (void)requestBreed:(UIButton *)btn {
    
    ZX_WEAK_SELF
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"cropColumnBase"] = @"zppz";
    params[@"cropId"] = self.cropId;
    
    [NetWorkHelp yl_postDataWith:params url:URL_SPLIT(URL_AgricultureCircle, URL_AgricultureCircle_findIllnessByCroId) success:^(id respose) {
        
        if ([respose[@"header"][@"statusCode"] integerValue] == 200) {
            
            weakSelf.breedModelArray = [WHRPublishPostARewardModel mj_objectArrayWithKeyValuesArray:respose[@"body"][@"cropSubsectionConList"]];
            
            NSMutableArray<NSString *>  *mArray = [NSMutableArray<NSString *> array ];
            
            for (WHRPublishPostARewardModel *model in self.breedModelArray) {
                
                NSString *str = model.name;
                /** 品种id */
                self.pzId = model.subsectionConId;
                [mArray addObject:str];
            }
            
            zySheetPickerView *pickerView = [zySheetPickerView ZYSheetStringPickerWithTitle:mArray  andHeadTitle:@"选择作物" Andcall:^(zySheetPickerView *pickerView, NSString *choiceString) {
                
                weakSelf.pz = choiceString;
                [btn setTitle:choiceString forState:UIControlStateNormal];
                
                [pickerView dismissPicker];
            }];
            [pickerView show];
            
            
        } else if ([respose[@"header"][@"statusCode"] integerValue] != 200) {
            
        }
        
        
    } failure:^(NSError *error) {
        
        
        
    }];
    
    
    
}


#pragma mark - Action

- (void)backAction:(UIBarButtonItem *)item {
    [self.navigationController popViewControllerAnimated:YES];
}



#pragma mark - tableviewDlegate & tableviewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    AXMyAddressSelectCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    ZX_WEAK_CELL(cell)
    
    
    if (indexPath.row == 0) {
        
        ZX_WEAK_SELF
        
        weakSelf.AddressPickerView  = [CZHAddressPickerView cityPickerViewWithProvince:@"" city:@"" cityBlock:^(NSString *province, NSString *city) {
            
            [weakCell.addressBtn setTitle:[NSString stringWithFormat:@"%@%@",province,city] forState:UIControlStateNormal];
            NSLog(@"%@%@",province,city);
            
        }];
        
        weakSelf.AddressPickerView.choice_CropId = ^(NSString *cropOneId, NSString *cropTwoId) {
            
            weakSelf.cropId = cropTwoId;
            
            NSLog(@"%@%@",cropOneId,cropTwoId);
        };
    }
    
    
    else if (indexPath.row == 1) {
        
        /** 请求品种 */
        [self requestBreed:weakCell.addressBtn];
    }
    
   else if (indexPath.row == 2) {
        
        NSDate *now = [NSDate date];
        NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
        fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
        NSString *nowStr = [fmt stringFromDate:now];
        [CGXPickerView showDatePickerWithTitle:@"种植时间" DateType:UIDatePickerModeDate DefaultSelValue:nil MinDateStr:@"1900-01-01 00:00:00" MaxDateStr:nowStr IsAutoSelect:YES Manager:nil ResultBlock:^(NSString *selectValue) {
            [weakCell.addressBtn setTitle:selectValue forState:UIControlStateNormal];
            
            NSLog(@"%@",selectValue);
           
        }];
    }
    
}

//几个分区
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 14;
}

/** cell的高度 */

/** cell 自适应第二步 */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    

    return 44;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    
    //判断选择
    if (indexPath.row == 0||indexPath.row == 1||indexPath.row == 2||indexPath.row == 3||indexPath.row == 4||indexPath.row == 5||indexPath.row == 6||indexPath.row == 10||indexPath.row == 11||indexPath.row == 12) {
        
        AXMyAddressSelectCell *farmerSexCell = [tableView dequeueReusableCellWithIdentifier:[AXMyAddressSelectCell reuseID] forIndexPath:indexPath];
        [farmerSexCell setSelectionStyle:UITableViewCellSelectionStyleNone];
        
        //作物名称选择

        if (indexPath.row == 0) {
          
            ZX_WEAK_CELL(farmerSexCell)
            
            
            farmerSexCell.titleLabel.text = @"作物名称";
            [farmerSexCell.addressBtn setTitle:@"选择作物" forState:UIControlStateNormal];
            farmerSexCell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
            /** 请选择作物按钮 */
            farmerSexCell.tapButtonClickedBlock = ^(NSInteger index) {
                
                ZX_WEAK_SELF
                
                weakSelf.AddressPickerView  = [CZHAddressPickerView cityPickerViewWithProvince:@"" city:@"" cityBlock:^(NSString *province, NSString *city) {
                    
                    [weakCell.addressBtn setTitle:[NSString stringWithFormat:@"%@%@",province,city] forState:UIControlStateNormal];
                    NSLog(@"%@%@",province,city);
                    
                }];
                
                 weakSelf.AddressPickerView.choice_CropId = ^(NSString *cropOneId, NSString *cropTwoId) {
                    
                    weakSelf.cropId = cropTwoId;
                    
                    NSLog(@"%@%@",cropOneId,cropTwoId);
                };
                
            };
            
            
        }
        
        /** 品种 */
        else if (indexPath.row == 1){
            
      ZX_WEAK_CELL(farmerSexCell)
      
            farmerSexCell.titleLabel.text = @"作物品种";
            [farmerSexCell.addressBtn setTitle:@"选择作物" forState:UIControlStateNormal];
            farmerSexCell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
           
            farmerSexCell.tapButtonClickedBlock = ^(NSInteger index) {
                
                /** 请求品种 */
                [self requestBreed:weakCell.addressBtn];
             
            };
            
     
          
            
            
        }
        
        else if (indexPath.row == 2) {
            
     // ZX_WEAK_SELF
      
            //[farmerSexCell.addressBtn setTitle:@"2018-10-10" forState:UIControlStateNormal];
            farmerSexCell.titleLabel.text = @"种植时间";
            [farmerSexCell.addressBtn setTitle:@"选择种植时间" forState:UIControlStateNormal];
            farmerSexCell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
            /** 种植时间的选泽方法 */
               farmerSexCell.tapButtonClickedBlock = ^(NSInteger index) {
                   
                   AXMyAddressSelectCell *cell = [tableView cellForRowAtIndexPath:indexPath];
                   
                   ZX_WEAK_CELL(cell)
                   
                   NSDate *now = [NSDate date];
                   NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
                   fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
                   NSString *nowStr = [fmt stringFromDate:now];
                   [CGXPickerView showDatePickerWithTitle:@"种植时间" DateType:UIDatePickerModeDate DefaultSelValue:nil MinDateStr:@"1900-01-01 00:00:00" MaxDateStr:nowStr IsAutoSelect:YES Manager:nil ResultBlock:^(NSString *selectValue) {
                       [weakCell.addressBtn setTitle:selectValue forState:UIControlStateNormal];
                       
                       NSLog(@"%@",selectValue);
                       
                   }];
                   
                 
                   
               };
            
    
            
      
            
           
            
            
        }
        
        
        else if (indexPath.row == 3) {
            
            
            
            [farmerSexCell.addressBtn setTitle:@"点击填写亩" forState:UIControlStateNormal];
            farmerSexCell.titleLabel.text = @"种植面积";
            farmerSexCell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
            
        }
        
        else if (indexPath.row == 4) {
            
            
            [farmerSexCell.addressBtn setTitle:@"云南省昆明市盘龙区" forState:UIControlStateNormal];
            farmerSexCell.titleLabel.text = @"种植地址";
            farmerSexCell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
            
        }
        
        
        else if (indexPath.row == 5) {
            
            
            
            [farmerSexCell.addressBtn setTitle:@"点击填写详细地址" forState:UIControlStateNormal];
            farmerSexCell.titleLabel.text = @"详细地址";
            farmerSexCell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
            
        }
        
        else if (indexPath.row == 6) {
            
            
            [farmerSexCell.addressBtn setTitle:@"绿色食品" forState:UIControlStateNormal];
            farmerSexCell.titleLabel.text = @"种植目标";
            farmerSexCell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
            
        }
        else if (indexPath.row == 10) {
            
            
            
            [farmerSexCell.addressBtn setTitle:@"添加" forState:UIControlStateNormal];
            farmerSexCell.titleLabel.text = @"病害";
            farmerSexCell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
            farmerSexCell.tapButtonClickedBlock = ^(NSInteger index) {
              
                AXMyFormerYearsDiseaseVC *vc = [[AXMyFormerYearsDiseaseVC alloc]init];
                [self.navigationController pushViewController:vc animated:YES];
            };
            
        }
        else if (indexPath.row == 11) {
            
            
            [farmerSexCell.addressBtn setTitle:@"添加" forState:UIControlStateNormal];
            farmerSexCell.titleLabel.text = @"虫害";
            farmerSexCell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
            
        }
        else if (indexPath.row == 12) {
            

            [farmerSexCell.addressBtn setTitle:@"添加" forState:UIControlStateNormal];
            farmerSexCell.titleLabel.text = @"草害";
            farmerSexCell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
            
        }
        
        
        return farmerSexCell;
    }
    // 判断单位性质选择按钮
    else if (indexPath.row == 8) {
        
        AXMyButtonSelectCell *buttonSelectCell = [tableView dequeueReusableCellWithIdentifier:[AXMyButtonSelectCell reuseID] forIndexPath:indexPath];
        buttonSelectCell.array = _array;
        [buttonSelectCell setSelectionStyle:UITableViewCellSelectionStyleNone];
        return buttonSelectCell;
    }
    
    else if (indexPath.row == 7) {
        
        AXMyTitleCell *titleCell = [tableView dequeueReusableCellWithIdentifier:[AXMyTitleCell reuseID] forIndexPath:indexPath];
        [titleCell setSelectionStyle:UITableViewCellSelectionStyleNone];
        titleCell.titleLabel.text = @"种植方式";
        return titleCell;
        
    }
    
    
    

    
    else  if (indexPath.row == 9) {
        
        AXMyTitleCell *titleCell = [tableView dequeueReusableCellWithIdentifier:[AXMyTitleCell reuseID] forIndexPath:indexPath];
        [titleCell setSelectionStyle:UITableViewCellSelectionStyleNone];
        titleCell.titleLabel.text = @"往年病虫草害";
        return titleCell;
        
    
    }
    
  
    
    AXMyNewFarmerSubmitCell *submitCell = [tableView dequeueReusableCellWithIdentifier:[ AXMyNewFarmerSubmitCell reuseID] forIndexPath:indexPath];
    [submitCell setSelectionStyle:UITableViewCellSelectionStyleNone];
    submitCell.saveBtn.hidden = NO;
    submitCell.saveBtn.cornerRadius =5;
    submitCell.SelectBtn.hidden = YES;
    submitCell.agreeLabel.hidden = YES;
    submitCell.submiBtn.hidden = YES;
   
    submitCell.tapButtonClickedBlock = ^(NSInteger index) {
        
        
        AXMyCheckProgressVC *vc = [[AXMyCheckProgressVC alloc] initWithNibName:@"AXMyCheckProgressVC" bundle:[NSBundle mainBundle]];
        [self.navigationController pushViewController:vc animated:YES];
        
    };
    
    return submitCell;
    
    
    
}

#pragma mark -MCPickerViewDelegate

- (void)MCPickerView:(MCPickerView *)MCPickerView complete:(NSString *)complete
{
   
    NSLog(@"===%@",complete);
}
- (void)MCPickerView:(MCPickerView *)MCPickerView didSelcetedRow:(NSInteger)Row value:(MCPickerModel *)value
{
    if (Row == 0) {
        __block NSMutableArray *models = [NSMutableArray array];
        for (int i=0; i<5; i++) {
            MCPickerModel *model = [[MCPickerModel alloc]init];
            model.name = [NSString stringWithFormat:@"作物%d",i];
            model.pid = @"123";
            [models addObject:model];
        }
        self.picker.dataArray = models;
    }else if(Row == 1){
        __block NSMutableArray *models = [NSMutableArray array];
        for (int i=0; i<5; i++) {
            MCPickerModel *model = [[MCPickerModel alloc]init];
            model.name = [NSString stringWithFormat:@"葡萄%d",i];
            model.pid = @"123";
            [models addObject:model];
        }
        self.picker.dataArray = models;
    }
}


#pragma mark - delegate

///** 选中时间的方法 */
//
//- (void)didClickFinishDateTimePickerView:(NSString *)date{
//
//   self.timeStr = date;
//    NSLog(@"%@", self.timeStr);
//
//}





@end
